<template>
  <el-card>
    <div class="header-box">
      <!-- 关键词-->
      <div>
        <el-input
          v-model="queryInfo.keywords"
          clearable
          placeholder="请输入搜索关键字"
          style="width: 180px"
          @change="handleResetPage"
        >
        </el-input>
      </div>
      <!-- 用户搜索 -->
      <div>
        <el-select
          v-model="queryInfo.member_id"
          :loading="searchItem.member_id.loading"
          :remote-method="handleSearchMember"
          clearable
          filterable
          placeholder="请选择用户"
          remote
          reserve-keyword
          style="width: 180px"
          @change="handleResetPage"
        >
          <el-option
            v-for="item in searchItem.member_id.options"
            :key="item.member_id"
            :label="item.account + `(${item.nickname})`"
            :value="item.member_id"
          >
          </el-option>
        </el-select>
      </div>
      <!-- 商家 -->
      <div>
        <el-select
          v-model="queryInfo.provider_id"
          :loading="searchItem.provider_id.loading"
          :remote-method="handleSearchProvider"
          clearable
          filterable
          placeholder="请选择商家"
          remote
          reserve-keyword
          @change="handleResetPage('provider')"
        >
          <el-option
            v-for="item in searchItem.provider_id.options"
            :key="item.provider_id"
            :label="item.name + `(${item.account})`"
            :value="item.provider_id"
          >
          </el-option>
        </el-select>
      </div>
      <!--       商品-->
      <div v-if="queryInfo.provider_id">
        <el-select
          v-model="queryInfo.goods_id"
          :loading="searchItem.goods_id.loading"
          :remote-method="handleSearchGoods"
          clearable
          filterable
          placeholder="请选择商品"
          remote
          style="width: 180px"
          @change="handleResetPage"
        >
          <el-option
            v-for="item in searchItem.goods_id.options"
            :key="item.goods_id"
            :label="item.name"
            :value="item.goods_id"
          >
          </el-option>
        </el-select>
      </div>

      <div>
        <el-select
          v-model="queryInfo.pickup_method"
          clearable
          placeholder="请选择交付方式"
          style="width: 180px"
          @change="handleResetPage"
        >
          <el-option
            v-for="(item, index) in filterItem.pickup_method"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-select
          v-model="queryInfo.status"
          clearable
          placeholder="请选择状态"
          style="width: 180px"
          @change="handleResetPage"
        >
          <el-option
            v-for="(item, index) in filterItem.status"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-select
          v-model="queryInfo.refund"
          clearable
          placeholder="请选择退单状态"
          style="width: 180px"
          @change="handleResetPage"
        >
          <el-option
            v-for="(item, index) in filterItem.refund"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" @click="handleResetPage">
          搜索
          <i class="el-icon-search el-icon--right"></i>
        </el-button>
      </div>
      <div class="add">
        <el-button type="primary" @click="handleExport()"> 导出</el-button>
      </div>
    </div>

    <div class="flex-content">
      <el-table
        v-loading="isLoading.table"
        :data="dataList"
        style="width: 100%"
      >
        <el-table-column
          align="center"
          label="订单号"
          prop="order_sn"
          width="175"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="下单用户"
          prop="member"
          show-overflow-tooltip
          width="200"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="下单商家"
          prop="provider"
          show-overflow-tooltip
          width="200"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="交付方式"
          prop="pickupMethod.type"
          width="125"
        >
          <template v-slot="{ row }">
            <el-tooltip
              :content="row.pickupMethod.info"
              class="item"
              effect="dark"
              placement="right"
            >
              <el-tag>
                {{ row.pickupMethod.type }}
              </el-tag>
            </el-tooltip>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="订单总金额"
          prop="order_fee"
          width="125"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="商品金额"
          prop="goods_fee"
          width="125"
        >
        </el-table-column>

        <el-table-column align="center" label="运费" prop="freight" width="125">
        </el-table-column>

        <el-table-column
          align="center"
          label="付款情况"
          prop="payInfo"
          width="125"
        >
        </el-table-column>

        <el-table-column align="center" label="订单状态" width="150">
          <template v-slot="{ row }">
            <el-tag size="mini" type="primary">
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="下单时间"
          prop="create_time"
          width="150"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="更新时间"
          prop="update_time"
          width="150"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="发货时间"
          prop="taking_time"
          width="150"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="完结时间"
          prop="complete_time"
          width="150"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="备注"
          prop="remark"
          show-overflow-tooltip
          width="200"
        >
        </el-table-column>

        <el-table-column align="center" fixed="right" label="操作" width="225">
          <template v-slot="{ row }">
            <el-button
              v-if="row.operate.isShowFreightEdit === 1"
              size="mini"
              type="primary"
              @click="handleEdit(row)"
            >
              编辑运费
            </el-button>
            <el-button size="mini" type="primary" @click="handleInfo(row)">
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页-->
    <div class="footer-box">
      <pagination
        :limit.sync="queryInfo.rows"
        :page.sync="queryInfo.page"
        :total="totalCount"
        @pagination="getData"
      >
      </pagination>
    </div>

    <!-- 添加与修改-->
    <Edit v-if="isDialog.edit" :info="dataInfo" @close="closeDialog"></Edit>
    <!-- 详情-->
    <Info v-if="isDialog.info" :info="dataInfo" @close="closeDialog"></Info>
  </el-card>
</template>

<script>
import { lists as getList, orderExport } from "@/api/order";
import Edit from "./components/edit.vue";
import Info from "./components/info.vue";
import { lists as searchMember } from "@/api/member";
import { lists as searchGoods } from "@/api/goods";
import { lists as searchProvider } from "@/api/provider";

export default {
  name: "Order",
  components: {
    Edit,
    Info,
  },
  data() {
    return {
      // 查询条件
      queryInfo: {
        page: 1,
        rows: 10,
        keywords: "",
        bs_id: 1,
        member_id: "",
        goods_id: "",
        provider_id: "",
        pickup_method: "",
        status: "",
        refund: "",
      },
      // 总页数
      totalCount: 0,
      // 列表
      dataList: [],
      // 详情
      dataInfo: {},
      // 筛选项
      filterItem: {
        status: [],
        refund: [],
      },
      // 远程搜索项
      searchItem: {
        member_id: {
          loading: false,
          options: [],
        },
        goods_id: {
          loading: false,
          options: [],
        },
        provider_id: {
          loading: false,
          options: [],
        },
      },
      // 加载
      isLoading: {
        table: false,
      },
      // 弹窗
      isDialog: {
        edit: false,
        info: false,
      },
      // 防抖
      queryId: +new Date(),
    };
  },
  created() {
    this.queryInfo.status = this.$route.query.status
      ? Number(this.$route.query.status)
      : "";
    this.handleSearchMember();
    this.handleSearchProvider();
    this.getData();
  },
  methods: {
    // 列表
    getData() {
      this.isLoading.table = true;
      this.queryId++;
      let queryId = this.queryId;
      getList(this.queryInfo)
        .then((res) => {
          if (queryId !== this.queryId) {
            return;
          }
          this.dataList = res.data.list;
          this.totalCount = res.data.total;
          this.filterItem.status = res.data.status_screen;
          this.filterItem.refund = res.data.refund_screen;
          this.filterItem.pickup_method = res.data.pickup_method_screen;
        })
        .finally(() => {
          this.isLoading.table = false;
        });
    },
    // 编辑
    handleEdit(row) {
      this.dataInfo = row;
      this.isDialog.edit = true;
    },
    // 详情
    handleInfo(row) {
      this.dataInfo = row;
      this.isDialog.info = true;
    },
    // 导出
    handleExport() {
      orderExport(this.queryInfo).then((res) => {
        let path = res.data.path;
        window.open(path);
      });
    },
    // 远程搜索 - 用户
    handleSearchMember(query) {
      this.searchItem.member_id.loading = true;
      searchMember({
        page: 1,
        rows: 10,
        keywords: query ? query : "",
      }).then((res) => {
        this.searchItem.member_id.loading = false;
        this.searchItem.member_id.options = res.data.list;
      });
    },
    // 商品
    handleSearchGoods(query) {
      this.searchItem.goods_id.loading = true;
      searchGoods({
        page: 1,
        rows: 10,
        provider_id: this.queryInfo.provider_id,
        keywords: query ? query : "",
      }).then((res) => {
        this.searchItem.goods_id.loading = false;
        this.searchItem.goods_id.options = res.data.list;
      });
    },
    // 商家
    handleSearchProvider(query) {
      this.searchItem.provider_id.loading = true;
      searchProvider({
        page: 1,
        rows: 10,
        keywords: query ? query : "",
      }).then((res) => {
        this.searchItem.provider_id.loading = false;
        this.searchItem.provider_id.options = res.data.list;
      });
    },
    // 重置页面数据
    handleResetPage(type = "") {
      this.queryInfo.page = 1;
      this.getData();
      if (type === "provider") {
        this.handleSearchGoods();
      }
    },
    //关闭
    closeDialog(val) {
      if (!val) {
        this.getData();
      }
      this.isDialog.edit = false;
      this.isDialog.info = false;
    },
  },
};
</script>

<style lang="scss" scoped></style>
